home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / Source / Foundation / Common / ZExceptions.cpp < prev    next >
Text File  |  1997-08-16  |  5KB  |  227 lines

  1. /*
  2.  *  File:       ZExceptions.cpp
  3.  *  Summary:       ANSI derived exception classes.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1996-1997 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):    
  10.  *
  11.  *         <2>     8/16/97    JDJ        Uses OSStatus instead of OSErr. (OSStatus is a long).
  12.  *         <1>     1/13/96    JDJ        Created
  13.  */
  14.  
  15. #include <ZExceptions.h>
  16.  
  17. #include <StdIO.h>
  18. #include <String.h>
  19.  
  20. #include <Errors.h>
  21. #include <Memory.h>
  22. #include <Resources.h>
  23.  
  24. #include <ZDebug.h>
  25. #include <ZStringUtils.h>
  26.  
  27.  
  28. // ===================================================================================
  29. //    Exception Classes
  30. // ===================================================================================
  31. #if !HAS_ANSI_EXCEPTIONS
  32.  
  33. //---------------------------------------------------------------
  34. //
  35. // TBaseException::~TBaseException
  36. //
  37. //---------------------------------------------------------------
  38. TBaseException::~TBaseException()
  39. {
  40. }
  41.  
  42.     
  43. //---------------------------------------------------------------
  44. //
  45. // TBaseException::TBaseException
  46. //
  47. // Note that this uses a char buffer to avoid allocating any memory
  48. // in the heap. (C++ will create a copy of the exception object
  49. // when a throw statement is executed).
  50. //
  51. //---------------------------------------------------------------
  52. TBaseException::TBaseException(const string& mesg)
  53. {
  54.     short len = mesg.length();                    
  55.     if (len > 255)
  56.         len = 255;
  57.     BlockMoveData(mesg.c_str(), mText, len);
  58.     mText[len] = '¥0';
  59. }
  60.  
  61.     
  62. //---------------------------------------------------------------
  63. //
  64. // TBaseException::what
  65. //
  66. //---------------------------------------------------------------
  67. const char* TBaseException::what() const
  68. {
  69.     return mText;
  70. }
  71.  
  72. #pragma mark -
  73.  
  74. #endif    // !HAS_ANSI_EXCEPTIONS
  75.  
  76. //---------------------------------------------------------------
  77. //
  78. // ThrowIfNil
  79. //
  80. //---------------------------------------------------------------
  81. void ThrowIfNil(const void* ptr)        
  82. {
  83.     if (ptr == nil)
  84.         throw TSysMemoryException();
  85. }
  86.  
  87.  
  88. //---------------------------------------------------------------
  89. //
  90. // ThrowOSErr
  91. //
  92. //---------------------------------------------------------------
  93. void ThrowOSErr(OSStatus err)
  94. {
  95.     ASSERT(err != noErr);
  96.     
  97.     ThrowIfOSErr(err);
  98. }
  99.  
  100.  
  101. //---------------------------------------------------------------
  102. //
  103. // ThrowIfOSErr
  104. //
  105. //---------------------------------------------------------------
  106. void ThrowIfOSErr(OSStatus err)
  107. {
  108.     if (err != noErr) {
  109.         char mesg[256];
  110.         
  111.         switch (err) {
  112.             case memFullErr:
  113.                 throw TSysMemoryException();
  114.                 break;
  115.                 
  116.             case resNotFound:
  117.             case resFNotFound:
  118.             case addResFailed:
  119.             case rmvResFailed:
  120.             case resAttrErr:
  121.             case mapReadErr:
  122.                 sprintf(mesg, LoadRavenString("Resource error #%d").c_str(), err);
  123.                 throw TSystemException(err, mesg);                // ・・ハuse a TResourceException?
  124.                 break;
  125.                 
  126.             case dirFulErr:
  127.             case dskFulErr:
  128.             case nsvErr:
  129.             case ioErr:
  130.             case fnOpnErr:
  131.             case eofErr:
  132.             case posErr:
  133.             case mFulErr:
  134.             case tmfoErr:
  135.             case fnfErr:
  136.             case wPrErr:
  137.             case fLckdErr:
  138.     
  139.             case vLckdErr:
  140.             case fBsyErr:
  141.             case dupFNErr:
  142.             case opWrErr:
  143.             case rfNumErr:
  144.             case gfpErr:
  145.             case volOffLinErr:
  146.             case permErr:
  147.             case volOnLinErr:
  148.             case nsDrvErr:
  149.             case noMacDskErr:
  150.             case wrPermErr:
  151.             case dirNFErr:
  152.             case tmwdoErr:
  153.             case volGoneErr:
  154.                 sprintf(mesg, LoadRavenString("File error #%d").c_str(), err);
  155.                 throw TSystemException(err, mesg);                // ・・ハuse a TFileException?
  156.                 break;
  157.                 
  158.             default:
  159.                 sprintf(mesg, LoadRavenString("Error #%d").c_str(), err);
  160.                 throw TSystemException(err, mesg);    
  161.         }
  162.     }
  163. }        
  164.  
  165.  
  166. //---------------------------------------------------------------
  167. //
  168. // ThrowIfResError
  169. //
  170. //---------------------------------------------------------------
  171. void ThrowIfResError()
  172. {
  173.     ThrowIfOSErr(ResError());
  174. }        
  175.  
  176.  
  177. //---------------------------------------------------------------
  178. //
  179. // ThrowIfMemError
  180. //
  181. //---------------------------------------------------------------
  182. void ThrowIfMemError()
  183. {
  184.     ThrowIfOSErr(MemError());
  185. }        
  186.  
  187.  
  188. //---------------------------------------------------------------
  189. //
  190. // ThrowIfResFail
  191. //
  192. //---------------------------------------------------------------
  193. void ThrowIfResFail(const void* ptr)                                        
  194. {
  195.     if (ptr == nil) 
  196.         throw TSystemException(-192, "Resource was nil");    // ・・ハUse TResourceException?
  197.         
  198.     ThrowIfOSErr(ResError());
  199. }        
  200.  
  201.  
  202. //---------------------------------------------------------------
  203. //
  204. // ThrowIfMemFail
  205. //
  206. //---------------------------------------------------------------
  207. void ThrowIfMemFail(const void* ptr)
  208. {
  209.     if (ptr == nil) 
  210.         throw TSysMemoryException();
  211.         
  212.     ThrowIfOSErr(MemError());
  213. }        
  214.  
  215.  
  216. //---------------------------------------------------------------
  217. //
  218. // ThrowIfQDError
  219. //
  220. //---------------------------------------------------------------
  221. void ThrowIfQDError()
  222. {
  223.     ThrowIfOSErr(QDError());
  224. }
  225.  
  226.  
  227.